package com.coreJava.test01;

import java.util.Random;
import java.util.concurrent.*;

/**
 * 作者: LCG
 * 日期: 2019/9/4 09:04
 * 描述: 线程池测试代码
 */
public class XianChengChi {
    public static void main(String[] args) {

        /*ArrayBlockingQueue<Runnable> linkedBlockingQueue=new ArrayBlockingQueue<>(1);

        ThreadPoolExecutor threadPoolExecutor=new ThreadPoolExecutor(4,4,10,TimeUnit.SECONDS,linkedBlockingQueue);

        FutureTask<String> futureTask=new FutureTask<String>(new Callable<String>() {
            @Override
            public String call() throws Exception {

                Random random=new Random();

                int nextInt = random.nextInt(10000);

                System.out.println(nextInt);

                Thread.sleep(nextInt);
                return ""+random.nextInt();
            }
        });

        threadPoolExecutor.execute(futureTask);
        threadPoolExecutor.execute(futureTask);
        threadPoolExecutor.execute(futureTask);
        threadPoolExecutor.execute(futureTask);

        System.out.println(threadPoolExecutor.getActiveCount());
        System.out.println(threadPoolExecutor.getTaskCount());*/

        //单个线程的线程池
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        //定长的线程池
        Executors.newFixedThreadPool(10);
        //可调度的线程池
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);

       /*
       //多久后开始运行只运行一次
       ScheduledFuture<?> schedule = scheduledExecutorService.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("========");
            }
        }, 5, TimeUnit.SECONDS);
        */

        //多久后开始运行按照固定的频率运行
        scheduledExecutorService.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                System.out.println("====02====");
            }
        },5,5,TimeUnit.SECONDS);

        //scheduledExecutorService.shutdown();

    }
}
